Method Chaining
   HOME

TheInfoList



OR:

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in
object-oriented programming languages Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of p ...
. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.


Rationale

Local variable In computer science, a local variable is a variable that is given ''local scope''. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with o ...
declarations are
syntactic sugar In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
. Method chaining eliminates an extra variable for each intermediate step. The developer is saved from the cognitive burden of naming the variable and keeping the variable in mind. Method chaining has been referred to as producing a "train wreck" due to the increase in the number of methods that come one after another in the same line that occurs as more methods are chained together. A similar syntax is method cascading, where after the method call the expression evaluates to the current object, not the
return value In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is sa ...
of the method. Cascading can be implemented using method chaining by having the method return the current object itself. Cascading is a key technique in
fluent interface In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric ...
s, and since chaining is widely implemented in object-oriented languages while cascading isn't, this form of "cascading-by-chaining by returning " is often referred to simply as "chaining". Both chaining and cascading come from the Smalltalk language. While chaining is syntax, it has semantic consequences, namely that requires methods to return an object, and if implementing cascading via chaining, this must be the current object. This prevents the return value from being used for some other purpose, such as returning an error value.


Examples

A common example is iostream in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, where for example << returns the left object, allowing chaining. Compare: a << b << c; equivalent to: a << b; a << c; Another example in
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
uses the built-in methods of Array: somethings .filter(x => x.count > 10) .sort((a, b) => a.count - b.count) .map(x => x.name)


See also

*
Fluent interface In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric ...
*
Pipeline (Unix) In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process ('' std ...
*
Nesting (computing) In computing science and informatics, nestinghttps://study.com/academy/lesson/nesting-loops-statements-in-c-programming.html, title=Nesting Loops & Statements in C Programming is where information is organized in layers, or where objects contain ...
*
Builder pattern The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from it ...
* Pyramid of doom (programming)


References


External links


Creating DSLs in Java using method chaining concept

Method Chaining in PHP
{{DEFAULTSORT:Method Chaining Method (computer programming) Articles with example C++ code Articles with example Ruby code Articles with example Java code Articles with example PHP code